/**
 * 
 */
package org.vsg.stock.basic.persistence.hibernate;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.springframework.dao.DataAccessException;
import org.vsg.common.hibernate.AbstractGenericDaoImpl;
import org.vsg.common.hibernate.DateTransExtensionFunction;
import org.vsg.stock.basic.domain.StockDayIndicator;
import org.vsg.stock.basic.persistence.StockDayIndicatorDao;

/**
 * @author vison
 *
 */
public class StockDayIndicatorDaoImpl extends AbstractGenericDaoImpl implements StockDayIndicatorDao {


	
	
	@Override
	public StockDayIndicator getDayIndicator(String code, Date date)
			throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("SELECT ");
		query.append("entity ");
		query.append("FROM ").append(StockDayIndicator.class.getName());
		query.append(" as entity ");
		query.append("WHERE entity.stockCode = ? AND entity.stockDate >= ? AND entity.stockDate < ? ");

		Collection params = new Vector();
		params.add( code );
		params.add( DateTransExtensionFunction.changeFirstTimeOfDate( date ) );
		params.add( DateTransExtensionFunction.changeLastTimeOfDate( date ) );
		
		logger.debug("query : " + query.toString());
		
		Iterator<StockDayIndicator> iter = getTemplate().iterate(query.toString() , params.toArray());
		
		StockDayIndicator indicator = null;
		if ( iter.hasNext() ) {
			indicator = iter.next();
		}
		
		
		return indicator;
	}

	@Override
	public List<StockDayIndicator> findDayIndicatorByDate(
			Date startDate, Date toDate) throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("SELECT ");
		query.append("entity ");
		query.append("FROM ").append(StockDayIndicator.class.getName());
		query.append(" as entity ");
		query.append("WHERE entity.stockDate >= ? AND entity.stockDate < ? ");
		query.append("ORDER BY entity.stockCode asc , entity.stockDate asc ");

		Collection params = new Vector();
		params.add( DateTransExtensionFunction.changeFirstTimeOfDate( startDate ) );
		params.add( DateTransExtensionFunction.changeLastTimeOfDate( toDate ) );
		
		logger.debug("query : " + query.toString());
		
		List<StockDayIndicator> results = new Vector<StockDayIndicator>();
		
		Iterator<StockDayIndicator> iter = getTemplate().iterate(query.toString() , params.toArray());
		
		StockDayIndicator indicator = null;
		while ( iter.hasNext() ) {
			indicator = iter.next();
			results.add(indicator);
		}
		
		return results;
	}

	
	
	
	@Override
	public List<StockDayIndicator> findAllDayIndicator(String stockCode)
			throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("SELECT ");
		query.append("entity ");
		query.append("FROM ").append(StockDayIndicator.class.getName());
		query.append(" as entity ");
		query.append("WHERE entity.stockCode = ? ");
		query.append("ORDER BY entity.stockDate desc ");

		Collection params = new Vector();
		params.add( stockCode );
		
		logger.debug("query : " + query.toString());
		
		List<StockDayIndicator> results = new Vector<StockDayIndicator>();
		
		Iterator<StockDayIndicator> iter = getTemplate().iterate(query.toString() , params.toArray());
		
		StockDayIndicator indicator = null;
		while ( iter.hasNext() ) {
			indicator = iter.next();
			results.add(indicator);
		}
		
		return results;
	}
	
	

	@Override
	public void saveAll(Collection<StockDayIndicator> entities)
			throws DataAccessException {
		// TODO Auto-generated method stub
		getTemplate().saveOrUpdateAll(entities);
	}


}
